import request from '/@/utils/request';

export function useUserApi() {
	return {
		// 查询用户列表
		listUser: (query: any) => {
			return request({
				url: '/system/user/page',
				method: 'get',
				params: query,
			});
		},
		// 获取用户精简信息列表
		listSimpleUsers: () => {
			return request({
				url: '/system/user/list-all-simple',
				method: 'get',
			});
		},
		// 查询用户详细
		getUser: (userId: any) => {
			return request({
				url: '/system/user/get?id=' + parseStrEmpty(userId),
				method: 'get',
			});
		},
		// 新增用户
		addUser: (data: any) => {
			return request({
				url: '/system/user/create',
				method: 'post',
				data: data,
			});
		},
		// 修改用户
		updateUser: (data: any) => {
			return request({
				url: '/system/user/update',
				method: 'put',
				data: data,
			});
		},
		// 删除用户
		delUser: (userId: any) => {
			return request({
				url: '/system/user/delete?id=' + userId,
				method: 'delete',
			});
		},
		// 导出用户
		exportUser: (query: any) => {
			return request({
				url: '/system/user/export',
				method: 'get',
				params: query,
				responseType: 'blob',
			});
		},
		// 用户密码重置
		resetUserPwd: (userId: any, password: any) => {
			const data = {
				userId,
				password,
			};
			return request({
				url: '/system/user/update-password',
				method: 'put',
				data: data,
			});
		},
		// 用户状态修改
		changeUserStatus: (userId: any, status: any) => {
			const data = {
				userId,
				status,
			};
			return request({
				url: '/system/user/update-status',
				method: 'put',
				data: data,
			});
		},
		// 查询用户个人信息
		getUserProfile: () => {
			return request({
				url: '/system/user/profile/get',
				method: 'get',
			});
		},
		// 修改用户个人信息
		updateUserProfile: (data: any) => {
			return request({
				url: '/system/user/profile/update',
				method: 'put',
				data: data,
			});
		},
		// 用户密码重置
		updateUserPwd: (oldPassword: any, newPassword: any) => {
			const data = {
				oldPassword,
				newPassword,
			};
			return request({
				url: '/system/user/profile/update-password',
				method: 'put',
				params: data,
			});
		},
		// 用户头像上传
		uploadAvatar: (data: any) => {
			return request({
				url: '/system/user/profile/update-avatar',
				method: 'post',
				data: data,
			});
		},
	};
}

// 转换字符串，undefined,null等转化为""
export function parseStrEmpty(str: any) {
	if (!str || str == 'undefined' || str == 'null') {
		return '';
	}
	return str;
}
